var bpack = new Ext.Application({
    name: 'bpack',
    tabletStartupScreen: 'img/startup-tablet.jpg',
    phoneStartupScreen: 'img/startup-phone.jpg',
    tabletIcon: 'img/icon-ipad.png',
    phoneIcon: 'img/icon-iphone.png',
    glossOnIcon: false,
    launch: function() {
        //This it the main Panel that holds all others
        bpack.views.cards = new Ext.Panel({
            layout    : 'card',
            fullscreen: true,
            cardSwitchAnimation: 'fade',
            //Items will appear in the order they're written. The top item will appear first
            //Not representing a card here in the list blocks it from the application.
            items: [
                bpack.views.signin.card
            ]
        });
    }
});

bpack.views.signin = {};
bpack.views.signin.signinForm = new Ext.form.FormPanel({
    labelWidth:80,
    url:'https://p2p.b-pack.com/a/endemo/api3/oauth2/token',
    //url: 'login.php',
    frame:true, 
    title:'Please Login', 
    defaultType:'textfield',
    monitorValid:true,
    dockedItems: [{
        xtype: 'toolbar',
        title: 'Welcome To b-pack!',
        ui: 'light'
    }],
    items: [{
        xtype: 'fieldset',
        id: 'loginFormSet',
        title: '',
        items: [
            {
                xtype: 'emailfield',
                placeHolder: 'Username',
                name: 'username',
                id: 'Username',
                required: true
            }, {
                xtype: 'passwordfield',
                placeHolder: 'Password',
                name: 'password',
                required: true
            }, {
                xtype: 'checkboxfield',
                id: 'RememberMe',
                name: 'RememberMe',
                label: 'Save login?',
                labelWidth: '40%'
            }, {
		xtype: 'button',
		text:'Login',
		formBind: true,	 
		handler:function(){ 
		    bpack.views.signin.signinForm.submit({ 
			method:'POST',
			params: {client_secret:fe29cbe25949a49f51ed, client_id:bpackmobile, grant_type:password},
			waitTitle:'Connecting', 
			waitMsg:'Sending data...',
    
			success:function(){ 
				Ext.Msg.alert('Status', 'Login Successful!', function(btn, text){
				   if (btn == 'ok'){
					var redirect = 'mainapp/index.html'; 
					window.location = redirect;
				   }
				});
			},
    
			failure:function(form, action){ 
			    if(action.failureType == 'server'){ 
				obj = Ext.util.JSON.decode(action.response.responseText); 
				Ext.Msg.alert('Login Failed!', obj.errors.reason); 
			    }else{ 
				Ext.Msg.alert('Warning!', 'Authentication server is unreachable : ' + action.response.responseText); 
			    } 
			    bpack.views.signin.signinForm.reset(); 
			} 
		    }); 
		}
	    }
        ]
	
    }]
})
bpack.views.signin.card = new Ext.Panel({
    id: 'signinCard',
    items: [bpack.views.signin.signinForm]
    
})